package com.fruit.controller.app.rights;

import com.fruit.controller.base.BaseController;
import com.fruit.pojo.goods.RightConfig;
import com.fruit.pojo.rights.UserGoodsRights;
import com.fruit.service.goods.GoodsManager;
import com.fruit.service.rights.UserRightsManager;
import com.fruit.util.ResultInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@Controller
@RequestMapping(value = "/app/user/rights")
public class UserGoodsRightControllerApp extends BaseController {
	
	@Autowired
    private UserRightsManager userRightsManager;
    
	@Autowired
	private GoodsManager goodsManager;
	
	
	/**
	 * 权益首页
	 * @param page
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/index", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> index(String userId,int page,int showNum,HttpServletRequest request,HttpSession session) {
		this.logBefore(logger, "权益首页-开始");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> pam = new HashMap<String, Object>();
		pam.put("userId",userId);
		pam.put("start", (page-1) * showNum);
		pam.put("num", showNum);
		try {
			//查询每个大类的统计百分比 
			List<RightConfig> rightConfig =  userRightsManager.getClassPercentage(Integer.parseInt(userId)); 
			//权益列表
			List<RightConfig> userGoodsRightsList = userRightsManager.getGoodsRightList(pam);
			resultMap.put("rightConfig",rightConfig);
			resultMap.put("goodsList",userGoodsRightsList);
			resultMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resultMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resultMap;
	}

    /**
     * 获取用户权益列表
     *
     * @param id 权益信息ID
     * @return 权益信息详情
     */
    @RequestMapping(value = "/fruit", method = {RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> fruit(String userId,String classId,int page,int showNum,HttpServletRequest request,HttpSession session) {
		this.logBefore(logger, "权益列表");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> pam = new HashMap<String, Object>();
		pam.put("uId", userId);//用户id
		pam.put("classId", goodsManager.getClassIdByPid(classId));//总的分类id
		pam.put("start", (page-1) * showNum);
		pam.put("num", showNum);
		try {
			//获取权益列表
			List<UserGoodsRights> userGoodsRights = userRightsManager.userTrees(pam);//获取权益列表
			if(userGoodsRights == null){
				resultMap.put("result", this.initResult(ResultInfo.ERROR, "no", "请购买"));
			}
			
			resultMap.put("userGoodsRights",userGoodsRights);
			resultMap.put("result", this.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resultMap.put("result", this.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		this.logAfter(logger);
		return resultMap;
	}
    
}
